//
// Copyright (c) 2019-2023 BPer
//

#include "BPerJsonImpl.h"
#include <bperutil/sys/BPerLog.h>
#if BPer_Enable_Json

int32_t bPer_create_jsonReader(BPerJsonReader* reader,char* jsonStr){
	if(reader==NULL || jsonStr==NULL) return ERROR_JSON;

	reader->session=bPer_json_parse(jsonStr);
	if(reader->session==NULL)
		return bPer_error_wrap(ERROR_JSON,"json reader parse fail!");

	reader->session->outString=NULL;

	reader->getArraySize=bPer_json_getArraySize;
	reader->getArrayItem=bPer_json_getArrayItem;
	reader->getObjectItem=bPer_json_getObjectItem;
	reader->getObjectItemCaseSensitive=bPer_json_getObjectItemCaseSensitive;
	reader->hasObjectItem=bPer_json_hasObjectItem;
	reader->getErrorPtr=bPer_json_getErrorPtr;



	reader->getStringValue=bPer_json_getStringValue;
	reader->getNumberValue=bPer_json_getNumberValue;


	reader->isInvalid=bPer_json_isInvalid;
	reader->isFalse=bPer_json_isFalse;
	reader->isTrue=bPer_json_isTrue;
	reader->isBool=bPer_json_isBool;
	reader->isNull=bPer_json_isNull;
	reader->isNumber=bPer_json_isNumber;
	reader->isString=bPer_json_isString;
	reader->isArray=bPer_json_isArray;
	reader->isObject=bPer_json_isObject;
	reader->isRaw=bPer_json_isRaw;
	return BPer_Ok;
}
void bPer_destroy_jsonReader(BPerJsonReader* reader){
	if(reader==NULL) return;
	if(reader->session) bPer_json_delete(reader->session);
}

int32_t bPer_create_jsonWriter(BPerJsonWriter* writer){
	if(writer==NULL) return ERROR_JSON;
	writer->session=bPer_json_createObject();
	writer->createNull=bPer_json_createNull;
	writer->createTrue=bPer_json_createTrue;
	writer->createFalse=bPer_json_createFalse;
	writer->createBool=bPer_json_createBool;
	writer->createNumber=bPer_json_createNumber;
	writer->createString=bPer_json_createString;

	writer->createRaw=bPer_json_createRaw;
	writer->createArray=bPer_json_createArray;
	writer->createObject=bPer_json_createObject;

	writer->addNullToObject=bPer_json_addNullToObject;
	writer->addTrueToObject=bPer_json_addTrueToObject;
	writer->addFalseToObject=bPer_json_addFalseToObject;
	writer->addBoolToObject=bPer_json_addBoolToObject;
	writer->addNumberToObject=bPer_json_addNumberToObject;
	writer->addStringToObject=bPer_json_addStringToObject;
	writer->addRawToObject=bPer_json_addRawToObject;
	writer->addObjectToObject=bPer_json_addObjectToObject;
	writer->addArrayToObject=bPer_json_addArrayToObject;

	writer->print=bPer_json_print;
	writer->printUnformatted=bPer_json_printUnformatted;
	writer->printBuffered=bPer_json_printBuffered;

	//writer->free=bPer_json_free;
	return BPer_Ok;
}
void bPer_destroy_jsonWriter(BPerJsonWriter* writer){
	if(writer==NULL) return;
	if(writer->session) {

		if(writer->session->outString)
			bPer_json_free(writer->session->outString);

		bPer_json_delete(writer->session);
	}
}

#endif
